package com.best.onegous.View;

import org.json.JSONArray;
import org.json.JSONException;

import com.best.onegous.R;
import com.best.onegous.Rewrite.SlideShowView;
import com.best.onegous.Rewrite.SlideShowViews;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;

public class DetailsListview extends ListView implements AbsListView.OnScrollListener {
	private LinearLayout headerView;
	private int headerViewHeight;
	private SlideShowViews slideshowView;
	private int mFirstVisibleItem;
	private float startY;
	int scrollState;
	private MyOnScroll myOnScroll;
	private int yd = 0;

	public DetailsListview(Context context) {
		super(context);
		init(context);
	}

	public DetailsListview(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DetailsListview(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	public interface MyOnScroll {
		void myonScroll(int firstVisibleItem);
	}

	public void setMyOnScrollListener(MyOnScroll mOnScrollListener) {
		this.myOnScroll = mOnScrollListener;
	}

	private void init(Context context) {
		setOverScrollMode(View.OVER_SCROLL_NEVER);
		this.setOnScrollListener(this);
		headerView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.activity_shangpindetails_top_one,
				null, false);
		slideshowView = (SlideShowViews) headerView.findViewById(R.id.slideshowView);
		try {
			slideshowView.gundongimg(new JSONArray());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		measureView(headerView);
		headerViewHeight = headerView.getMeasuredHeight();
		// topPadding(-headerViewHeight);
		this.addHeaderView(headerView);
	}

	private void topPadding(int topPadding) {
		headerView.setPadding(headerView.getPaddingLeft(), headerView.getPaddingTop(), headerView.getPaddingRight(),
				topPadding);

	}

	private void topPaddings(int topPadding) {
		headerView.setPadding(headerView.getPaddingLeft(), topPadding, headerView.getPaddingRight(), topPadding);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		this.scrollState = scrollState;

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (null != myOnScroll) {
			myOnScroll.myonScroll(firstVisibleItem);
		}

		mFirstVisibleItem = firstVisibleItem;
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			if (mFirstVisibleItem == 0) {
				// ����ǰy��긳ֵ��startY��ʼy���
				startY = ev.getY();
				// startY =
				// headerView.getMeasuredHeight()-headerViewHeight-startY;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (mFirstVisibleItem == 0) {
				headmove(ev);
			}
			break;
		case MotionEvent.ACTION_UP:

			/*
			 * if(pd){ topPaddings(0); pd = false; }
			 */
			break;

		default:
			break;
		}
		return super.onTouchEvent(ev);
	}

	private void headmove(MotionEvent ev) {
		float offsetY = startY - ev.getY();

		// topPadding(headerView.getMeasuredHeight()-headerViewHeight);

		if (offsetY > 1) {
			if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
				topPadding(-(int) (offsetY / 2));
			}
			/*
			 * if(scrollState == SCROLL_STATE_TOUCH_SCROLL){
			 * topPadding(headerView.getPaddingBottom()-(int)(offsetY/2)); yd =
			 * yd - (int)(offsetY/2); }
			 */

		} else if (offsetY < -1) {
			if (headerView.getMeasuredHeight() < headerViewHeight) {

				topPadding(-(int) (offsetY / 2));

			} else {
				topPadding(0);
				yd = 0;
			}
		}

	}

	/**
	 * �򸸲���������С
	 * 
	 * @param child
	 */
	private void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
		int height;
		int tempHeight = p.height;

		if (tempHeight > 0) {
			height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
		} else {
			height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		}
		child.measure(width, height);
	}

}
